package com.oktest.measure.ui.fragment.work

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.cali.common.kt.gone
import com.cali.common.kt.value
import com.cali.libcore.base.BaseFragment
import com.chad.library.adapter.base.entity.node.BaseNode
import com.oktest.measure.MyApp
import com.oktest.measure.R
import com.oktest.measure.entity.Type
import com.oktest.measure.kt.resultListTryContext
import com.oktest.measure.kt.resultTryContext
import com.oktest.measure.ui.adapter.WorkTreeAdapter
import kotlinx.android.synthetic.main.fragment_history_detail.*
import kotlinx.android.synthetic.main.include_work_head_layout.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @Author HaoXin
 * @Date 2020/12/17
 */

class HistoryDetailFragment : BaseFragment() {

    companion object{
        const val WORK_ID = "work_id"
    }

    private var currentWorkId: Long = 0L

    private val workTreeAdapter by lazy {
        WorkTreeAdapter().also {
            launch(Dispatchers.Main){
                workDetailRecycler.layoutManager = LinearLayoutManager(context)
                workDetailRecycler.adapter = it
            }
        }
    }

    private fun initTreeRecyclerView() = launch {
        showTip("正在加载")
        withContext(Dispatchers.IO){
            MyApp.roomDatabase.xmlDao().load(currentWorkId)?.apply {
                val nodeList = ArrayList<BaseNode>()
                nodeList.addAll(MyApp.roomDatabase.xmlDao().devices(this.id))
                val nodes = workTreeAdapter.jobFlat(nodeList)
                launch(Dispatchers.Main) {
                    workTreeAdapter.data.addAll(nodes)
                    workTreeAdapter.notifyDataSetChanged()
                    toolbarHelper?.setTitleText(this@apply.name)
                }
            }
        }
        dismissTip()
    }

    override fun getLayoutId() = R.layout.fragment_history_detail

    override fun initData(savedInstanceState: Bundle?) {
        etCommentLayout.gone()
        currentWorkId = arguments.value(WORK_ID).toLong()
        initTreeRecyclerView()
        initTopLayout()
    }

    private fun initTopLayout() = launch {
        val typeList = resultListTryContext {
            val result = arrayListOf<Type>()
            MyApp.roomDatabase.apply {
                val devices = xmlDao().devices(currentWorkId)
                devices.forEach {
                    result.addAll(deviceDao().types(it.id))
                }
            }
            result
        }
        typeList.apply {
            // 全部点表
            twoAllTask.setTopText(this.size.toString())
            // 完成
            val doneNum = resultTryContext {
                var num = 0
                this.forEach {
                    if (it.isDone()) {
                        num++
                    }
                }
                num
            }
            twoDone.setTopText("$doneNum")
            // 剩余
            twoResidue.setTopText("${this.size - doneNum}")
        }
    }

    override fun hasToolbar(): Boolean {
        return true
    }

    override fun onBackPressedSupport(): Boolean {
        pop()
        return true
    }
}